package com.yumeno.market_goods.controller.pages.front;

import com.yumeno.market_goods.pojo.entity.User;
import com.yumeno.market_goods.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * 这是个登陆控制器，完成登录检验操作等功能
 */
@Controller
@RequestMapping("/pages/front")
public class LoginController {

    @Resource
    UserService userService;

    @RequestMapping("/loginPage")
    String loginPage() {
        return "pages/front/login/loginPage.html";
    }

    @RequestMapping("/login")
    ModelAndView login(User user) {
        ModelAndView mv = new ModelAndView();
        if (StringUtils.isEmpty(user.getPhone())) {
            mv.addObject("errMsg","手机号为空！");
            mv.setViewName("pages/front/login/loginPage.html");
        }
        boolean loginResult = userService.login(user);
        if (loginResult) {
            mv.addObject("msg","重定向");
            mv.setViewName("pages/back/backHome.html");
        } else {
            mv.addObject("errMsg","账号或密码错误！");
            mv.setViewName("pages/front/login/loginPage.html");
        }
        return mv;
    }
}
